package com.example.demo.controller;

import com.example.demo.model.User;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@RestController
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    // 临时token存储，生产环境应使用Redis等
    private final Map<String, String> tokenStore = new HashMap<>();

    @PostMapping("/login")
    public ResponseEntity<Map<String, Object>> login(@RequestBody Map<String, String> request) {
        String userId = request.get("user_id");
        Map<String, Object> response = new HashMap<>();

        // 特殊处理后厨登录
        if ("admin111111".equals(userId)) {
            response.put("is_admin", true);
        }

        User user = userService.login(userId);
        if (user == null) {
            response.put("result", false);
            response.put("message", "登录失败");
            return ResponseEntity.badRequest().body(response);
        }

        // 生成token
        String token = UUID.randomUUID().toString();
        tokenStore.put(token, userId);

        response.put("token", token);
        response.put("user_id", user.getUserId());
        response.put("result", true);
        return ResponseEntity.ok(response);
    }

    @GetMapping("/info")
    public ResponseEntity<Map<String, Object>> getUserInfo(
            @RequestParam String user_id,
            @RequestHeader(value = "Authorization", required = false) String token) {

        Map<String, Object> response = new HashMap<>();

        if (!validateToken(token, user_id)) {
            response.put("result", false);
            response.put("message", "无效的token或用户ID");
            return ResponseEntity.status(401).body(response);
        }

        User user = userService.getUserInfo(user_id);
        if (user == null) {
            response.put("result", false);
            response.put("message", "用户不存在");
            return ResponseEntity.status(404).body(response);
        }

        response.put("user_id", user.getUserId());
        response.put("result", true);
        return ResponseEntity.ok(response);
    }

    @PostMapping("/update")
    public ResponseEntity<Map<String, Object>> updateUserInfo(
            @RequestBody User user,
            @RequestHeader("Authorization") String token) {

        Map<String, Object> response = new HashMap<>();

        if (!validateToken(token, user.getUserId())) {
            response.put("result", false);
            response.put("message", "无效的token或用户ID");
            return ResponseEntity.status(401).body(response);
        }

        boolean success = userService.updateUserInfo(user);
        response.put("result", success);
        return success ? ResponseEntity.ok(response) : ResponseEntity.badRequest().body(response);
    }

    /**
     * 验证token有效性
     */
    private boolean validateToken(String token, String userId) {
        if (token == null || userId == null) {
            return false;
        }
        String storedUserId = tokenStore.get(token);
        return userId.equals(storedUserId);
    }
}